package exammanager;

import gui.CustomJButton;
import gui.GUIMain;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import structure.Exam;

public class GUIExamManagerLeft extends JPanel{

	private static final long serialVersionUID = 1L;
	private GUIExamManager parent;
	private GUIExamDisplay display;
	private CustomJButton addButton;
	private JPopupMenu menu;
	private int currentClickedRow;
	
	public GUIExamManagerLeft(GUIExamManager parent) {
		this.parent = parent;
		setOpaque(false);
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		display = new GUIExamDisplay();
		createPopupMenu();

		JPanel bottomPanel;
		bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		bottomPanel.setOpaque(false);
		addButton = new CustomJButton("Thêm", 100, 30, Color.WHITE, CustomJButton.DARKER,
				Color.darkGray, 2, false, true);
		addButton.addActionListener(new AddExamAction());
		bottomPanel.add(addButton);
		add(bottomPanel, BorderLayout.SOUTH);
		
		display.getJList().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		display.getJList().addListSelectionListener(new ExamSelectionListener());
		display.getJList().addMouseListener(new ExamMouseListener());
		add(display, BorderLayout.CENTER);
		refresh();
	}
	
	public void refresh(){
		display.getExamFromDB();
	}
	
	private void createPopupMenu(){
		menu  = new JPopupMenu();
		JMenuItem menuItem;
		menuItem = new JMenuItem("Chỉnh sửa...");
		menuItem.addActionListener(new ExamEdit());
		menu.add(menuItem);
		
		menuItem = new JMenuItem("Xoá");
		menuItem.addActionListener(new ExamDelete());
		menu.add(menuItem);
	}
	
	class ExamDelete extends AbstractAction{

		private static final long serialVersionUID = 1L;
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Exam ex = (Exam) display.getJList().getModel().getElementAt(currentClickedRow);
			Object[] options = {"Có",
                    "Không"};
			String content = "Bạn thực sự muốn xoá loại Bài thi '" + ex.getTitle() +"' ? ";
			int n = JOptionPane.showOptionDialog(parent,
				    content,
				    "Chú ý",
				    JOptionPane.YES_NO_OPTION,
				    JOptionPane.QUESTION_MESSAGE,
				    null,
				    options,
				    options);
			if(n == JOptionPane.YES_OPTION){
				if(GUIMain.getDb().DeleteExamDetailByExamID(ex.getExamID())){
					if(GUIMain.getDb().DeleteExam(ex.getExamID()))
					JOptionPane.showMessageDialog(parent, "Thao tác thành công", 
							"Thành công", JOptionPane.INFORMATION_MESSAGE);
					else{
						JOptionPane.showMessageDialog(parent, "Thao tác xoá loại câu hỏi không thành công", 
								"Lỗi", JOptionPane.ERROR_MESSAGE);
					}
				}else{
					JOptionPane.showMessageDialog(parent, "Thao tác xoá các câu hỏi không thành công", 
							"Lỗi",JOptionPane.ERROR_MESSAGE);
				}
				refresh();
				display.getJList().clearSelection();
				parent.getRightPanel().refresh(-1);
			}
		}
	}
	
	class AddExamAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				GUIExamAdd ex = new GUIExamAdd(parent.getFrame());
				ex.addWindowListener(new JDialogWindowListener());
				ex.pack();
				ex.setSize(300, ex.getHeight());
				ex.setLocationRelativeTo(parent);
				ex.setVisible(true);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(parent, e.getMessage(), 
						"Lỗi", JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
	}

	class ExamEdit extends AbstractAction{
		
		private static final long serialVersionUID = 1L;
		
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				Exam currentExam = display.getDisplayModel().getCollections().get(currentClickedRow);
				GUIExamEdit ex = new GUIExamEdit(parent.getFrame(), currentExam.getExamID());
				ex.addWindowListener(new JDialogWindowListener());
				ex.pack();
				ex.setSize(300, ex.getHeight());
				ex.setLocationRelativeTo(parent);
				ex.setVisible(true);
			} catch (Exception except) {
				JOptionPane.showMessageDialog(parent, except.getMessage(), 
						"Lỗi", JOptionPane.ERROR_MESSAGE);
				return;
			}
		}
	}
	
	class JDialogWindowListener extends WindowAdapter{
		
		@Override
		public void windowClosing(WindowEvent arg0) {
			super.windowClosing(arg0);
			refresh();
		}

		@Override
		public void windowClosed(WindowEvent e) {
			super.windowClosed(e);
			refresh();
		}
	}
	
	class ExamMouseListener extends MouseAdapter{

		@Override
		public void mouseClicked(MouseEvent e) {
			switch(e.getModifiers()) {
				case InputEvent.BUTTON1_MASK: {  
					super.mouseClicked(e);	
					break;
		        }
				case InputEvent.BUTTON2_MASK: {    
					break;
		        }
				case InputEvent.BUTTON3_MASK: {
					int temp = display.getJList().locationToIndex(e.getPoint());
					if(temp == -1) return;
			        currentClickedRow = temp;
			        menu.show(e.getComponent(), e.getX(), e.getY());
			        break;
		        }
			}
		}
	}
	
	class ExamSelectionListener implements ListSelectionListener{
		
		@Override
		public void valueChanged(ListSelectionEvent e) {
			if(!e.getValueIsAdjusting()){
				int ex = display.getJList().getSelectedIndex();
				if(ex==-1){
					parent.getRightPanel().refresh(-1);
					parent.getRightPanel().setAddButtonEnable(false);
				}else{
					
					parent.getRightPanel().refresh(display.getJList().getSelectedValue().getExamID());
					parent.getRightPanel().setAddButtonEnable(true);
				}
				
			}
		}
	}
}
